package com.mtons.mblog.modules.service.impl;

import com.mtons.mblog.modules.entity.People;
import com.mtons.mblog.modules.repository.PeopleRepository;
import com.mtons.mblog.modules.service.PeopleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Service
@Transactional(readOnly = true)
public class PeopleServiceImpl implements PeopleService {
    @Autowired
    private PeopleRepository peopleRepository;

    @Override
    public List<People> findAll() {
        List<People> all = peopleRepository.findAll();
        return all;
    }
    @Override
    public People findOne(Long id) {
        Optional<People> byId = peopleRepository.findById(id);
        return byId.get();
    }
    @Override
    @Transactional
    public void addOrUpdate(Map<String, String> body) {
        if (body == null) {
            return;
        }
        People one=null;
        if(body.get("id")!=null && !body.get("id").isEmpty()){
            Long aLong = new Long(body.get("id"));
            one = this.findOne(aLong);
            one.setAdress(body.get("adress"));
            one.setContent(body.get("content"));
            one.setUrl(body.get("url"));
            one.setName(body.get("name"));
            one.setParty_member(0);
        }else{
            one= new People();
            one.setAdress(body.get("adress"));
            one.setContent(body.get("content"));
            one.setUrl(body.get("url"));
            one.setName(body.get("name"));
            one.setParty_member(0);
            one.setCreted(new Date());
        }

        this.peopleRepository.save(one);
    }
}
